package com.ruoyi.dynamic.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.dynamic.mapper.RoomPermissionMapper;
import com.ruoyi.dynamic.model.pojo.entity.*;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomPostAddOrEditRequest;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomSingDelRequest;
import com.ruoyi.dynamic.service.*;
import com.ruoyi.system.api.RemoteAppUserService;
import com.ruoyi.system.api.model.user.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 房间权限 服务实现类
 * </p>
 *
 * @author Gnc
 * @since 2025-01-14
 */
@Service
public class RoomPermissionServiceImpl extends ServiceImpl<RoomPermissionMapper, RoomPermission> implements IRoomPermissionService {
    @Autowired
    private IRoomService iRoomService;
    @Autowired
    private RemoteAppUserService remoteAppUserService;
    @Autowired
    private IRoomUserPostService iRoomUserPostService;
    @Autowired
    private IRoomPostService iRoomPostService;
    @Autowired
    private IRoomUserPostPermissionService iRoomUserPostPermissionService;

    @Override
    public boolean addOrEdit(AppRoomPostAddOrEditRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());
        if (!verify.getUserId().equals(userId) && request.getPermissionIds().isEmpty()) {

        }

        R<AppUser> userInfo = remoteAppUserService.getUserInfo(request.getUserId(), SecurityConstants.APP);
        AppUser data = userInfo.getData();

        RoomPost post = iRoomPostService.verify(request.getPostId());
        RoomUserPost userPost = iRoomUserPostService.getById(request.getId());
        if (CommonUtils.isNull(userPost)) {
            userPost = new RoomUserPost();
        }
        userPost.setUserId(request.getUserId());
        userPost.setRoomId(request.getRoomId());
        userPost.setPostName(post.getName());
        userPost.setPostIconUrl(data.getSex().equals(0) ? post.getWoManIconUrl() : post.getManIconUrl());
        userPost.setUserPhoto(data.getPhoto());
        userPost.setUserNickName(data.getNickName());
        userPost.setPostId(request.getPostId());
        if (iRoomUserPostService.saveOrUpdate(userPost)) {
            Long id = userPost.getId();
            if (!request.getPermissionIds().isEmpty()) {
                iRoomUserPostPermissionService.lambdaUpdate()
                        .eq(RoomUserPostPermission::getUserPostId, userPost.getId())
                        .remove();

                List<RoomUserPostPermission> collect = request.getPermissionIds().stream().map(res -> {
                    RoomUserPostPermission userPostPermission = new RoomUserPostPermission();
                    userPostPermission.setPermissionId(res);
                    userPostPermission.setRoomId(request.getRoomId());
                    userPostPermission.setUserId(request.getUserId());
                    userPostPermission.setUserPostId(id);
                    return userPostPermission;
                }).collect(Collectors.toList());
                iRoomUserPostPermissionService.saveBatch(collect);
            }
        }
        return true;
    }

    @Override
    public boolean cancel(AppRoomSingDelRequest request, Long userId) {
        RoomUserPost verify = iRoomUserPostService.verify(request.getId());
        return iRoomUserPostService.removeById(request.getId());
    }

}
